package com.example.mobifin_busticket.views.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.Filter;
import android.widget.Filterable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.TextView;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.models.SourceModel;

class ViewHolder {
	public TextView tvItemName;
}

public class CustomListAdapter extends BaseAdapter implements Filterable {
	private final Context mcontext;
	ArrayList<SourceModel> mSourceList;
	private List<SourceModel> originalData = null;
	private List<SourceModel> filteredData = null;
	private ItemFilter mFilter = new ItemFilter();
	LayoutInflater vi;

	public CustomListAdapter(Context context, ArrayList<SourceModel> sourceList) {
		this.mcontext = context;
		mSourceList = sourceList;
		this.filteredData = sourceList;
		this.originalData = sourceList;
		vi = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return filteredData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return filteredData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		System.out.println("In Get View==>position:" + position);

		View v = convertView;

		if (v == null) {
			holder = new ViewHolder();
			v = vi.inflate(R.layout.row_setings, null);
			holder.tvItemName = (TextView) v.findViewById(R.id.tvItemName);

			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}

		String name = mSourceList.get(position).getName();
		// System.out.println(name);
		// holder.tvItemName.setText(name);
		holder.tvItemName.setText(filteredData.get(position).getName());
		return v;
	}

	public Filter getFilter() {
		// TODO Auto-generated method stub
		return mFilter;
	}

	private class ItemFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {

			String filterString = constraint.toString().toLowerCase();
			FilterResults results = new FilterResults();
			final List<SourceModel> list = originalData;

			int count = list.size();
			final ArrayList<SourceModel> sourcelist = new ArrayList<SourceModel>(
					count);
			SourceModel filterableString;

			for (int i = 0; i < count; i++) {
				filterableString = list.get(i);
				if (filterableString.getName().toLowerCase()
						.contains(filterString)) {
					sourcelist.add(filterableString);
				}
			}
			results.values = sourcelist;
			results.count = sourcelist.size();

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			filteredData = (ArrayList<SourceModel>) results.values;
			notifyDataSetChanged();
		}

	}

}